
package br.com.aeternu.controleatendimento.manutencao.agenda;

import java.awt.Rectangle;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.swing.table.DefaultTableModel;
import org.apache.log4j.Logger;
import br.com.aeternu.controleatendimento.common.ui.ControleLista;
import br.com.aeternu.controleatendimento.common.ui.EasyJFrame;
import br.com.aeternu.controleatendimento.common.ui.EasyOptionPane;
import br.com.aeternu.controleatendimento.excel.agenda.AgendaBean;
import br.com.aeternu.controleatendimento.excel.agenda.ExcelAgenda;
import br.com.aeternu.controleatendimento.exception.ControleAtendimentoException;
import br.com.aeternu.controleatendimento.manutencao.agenda.ui.IfcUIAgenda;
import br.com.aeternu.controleatendimento.manutencao.agenda.ui.UIAgenda;
import br.com.aeternu.controleatendimento.util.FormatUtil;

public class Agenda extends ControleLista implements IfcUIAgenda
{

   private static Logger logger = Logger.getLogger(Agenda.class);
   private ExcelAgenda excel;
   private UIAgenda ui;

   public Agenda() throws ControleAtendimentoException
   {
      this.excel = new ExcelAgenda();
      this.ui = new UIAgenda(this);

      carregaTela();
      atualizaList();
   }

   @Override
   public void atualizaPlanilha(EasyJFrame ui, int row, int column, Object newValue)
   {
      DateFormat df = new SimpleDateFormat(FormatUtil.DATE_FORMAT);
      UIAgenda uiAgenda = (UIAgenda) ui;

      try
      {
         List<AgendaBean> agendaBeanList = excel.obterRegistros();
         AgendaBean bean;

         if (!agendaBeanList.isEmpty())
         {
            try
            {
               bean = agendaBeanList.get(row);
            }
            catch (IndexOutOfBoundsException e)
            {
               // Lista não está vazia, porém o índice solicitado (row) ainda não foi preenchido
               bean = new AgendaBean();
            }
         }
         else
         {
            bean = new AgendaBean();
         }

         switch (column)
         {
            case 0:
               try
               {
                  bean.setData(df.parse((String) newValue));
               }
               catch (ParseException e)
               {
                  logger.error(e.getMessage(), e);
               }
               break;
            case 1:
               bean.setQtdeMatutino((Integer) newValue);
               break;
            case 2:
               bean.setQtdeVespertino((Integer) newValue);
               break;
         }

         excel.atualiza(bean);
         atualizaList();
      }
      catch (ControleAtendimentoException e)
      {
         logger.error(e.getMessage(), e);
         EasyOptionPane.showErrorMessageDialog(uiAgenda, e.getMessage());
      }
   }

   @Override
   public void incluirAgenda(UIAgenda ui)
   {
      try
      {
         this.excel.novaAgenda();
         atualizaList();
      }
      catch (ControleAtendimentoException e)
      {
         EasyOptionPane.showErrorMessageDialog(ui, e.getMessage());
      }
   }

   protected void atualizaList() throws ControleAtendimentoException
   {
      DateFormat df = new SimpleDateFormat(FormatUtil.DATE_FORMAT);
      DefaultTableModel model = (DefaultTableModel) ui.getTable().getModel();

      while (model.getRowCount() > 0)
      {
         for (int i = 0; i < model.getRowCount(); i++)
         {
            model.removeRow(i);
         }
      }

      List<AgendaBean> medicoBeanList = excel.obterRegistros();

      for (AgendaBean bean : medicoBeanList)
      {
         model.addRow(new Object[]{df.format(bean.getData()), bean.getQtdeMatutino(), bean.getQtdeVespertino()});
      }

      int row = model.getRowCount() - 1;

      Rectangle rect = ui.getTable().getCellRect(row, model.getColumnCount(), true);
      ui.getTable().addRowSelectionInterval(row, row);
      ui.getTable().scrollRectToVisible(rect);
      ui.getTable().requestFocusInWindow();
   }

   protected void carregaTela()
   {
      // Nada para carregar
   }

}
